import type { SoElements } from '../../../../../../types/constants/personalization/so'
import type { ThemeType } from '../../../../../../types/interfaces'
import { SearchThemeService } from '../../../../../../lib/abstract'
import { PageHandler } from '../../../../../../lib/page_handler'
import { SoPageType } from '../../../../../../lib/page_handler/platform/so/types'
import {
  UNDEFINED_PAGE_TYPE,
  UNDEFINED_PAGE_TYPE_TIPS,
} from '../../../../../../lib/page_handler/type'
import {
  BODY_SELECTOR,
  logger,
  loggerDeduplicator,
  PLATFORM_PAGE_TYPES,
  SUPPORTED_PLATFORMS,
} from '../../../../../../types/constants'
import { SoNotFoundPage } from './so/page/404'
import { SoNotFoundWenkuPage } from './so/page/404_wenku'
import { SoCommonPage } from './so/page/common'
import { SoGoodDoctorPage } from './so/page/deprecated/good_doctor'
import { SoPhotoPage } from './so/page/deprecated/photo'
import { SoQaPage } from './so/page/deprecated/qa'
import { SoEncyclopediaPage } from './so/page/encyclopedia'
import { SoIndexEncyclopediaPage } from './so/page/index_encyclopedia'
import { SoIndexSearchPage } from './so/page/index_search'
import { SoIndexVideoPage } from './so/page/index_video'
import { SoIndexWenkuPage } from './so/page/index_wenku'
import { SoNewsPage } from './so/page/news'
import { SoVideoPage } from './so/page/video'
import { SoWebPage } from './so/page/web'
import { SoWenkuPage } from './so/page/wenku'

/**
 * 页面处理器
 */
const PAGE_HANDLERS: Partial<Record<SoPageType, any>> = {
  [SoPageType.Web]: SoWebPage,
  [SoPageType.News]: SoNewsPage,
  [SoPageType.Video]: SoVideoPage,
  [SoPageType.Encyclopedia]: SoEncyclopediaPage,
  [SoPageType.Wenku]: SoWenkuPage,
  [SoPageType.IndexSearch]: SoIndexSearchPage,
  [SoPageType.IndexVideo]: SoIndexVideoPage,
  [SoPageType.IndexEncyclopedia]: SoIndexEncyclopediaPage,
  [SoPageType.IndexWenku]: SoIndexWenkuPage,
  [SoPageType.NotFound]: SoNotFoundPage,
  [SoPageType.WenkuNotFound]: SoNotFoundWenkuPage,
  [SoPageType.Qa]: SoQaPage,
  [SoPageType.Photo]: SoPhotoPage,
  [SoPageType.GoodDoctor]: SoGoodDoctorPage,
}

/**
 * 主题服务测试链接
 */
export class SoThemeService extends SearchThemeService {
  readonly name: string = 'SoTheme'
  readonly platformPageType: string = PLATFORM_PAGE_TYPES.SEARCH.value
  private static instance: SoThemeService
  declare readonly elements: SoElements
  private readonly commonPage: SoCommonPage = SoCommonPage.getInstance()
  private readonly pageHandler: PageHandler = PageHandler.getInstance()
  private currentPageType: SoPageType | 'default' = this.pageHandler.pages.so.pageType
  readonly wrapperSelector: string = BODY_SELECTOR

  constructor() {
    super(SUPPORTED_PLATFORMS.SO)

    this.updateElementStylesForTheme()
    this.checkPageType(this.currentPageType)
  }

  /**
   * 获取单例实例
   */
  static getInstance(): SoThemeService {
    if (!SoThemeService.instance) {
      SoThemeService.instance = new SoThemeService()
    }
    return SoThemeService.instance
  }

  /**
   * 批量初始化页面样式
   */
  updateElementStylesForTheme(): void {
    // 通用样式初始化
    this.commonPage.init()

    // 动态获取当前页面处理器并初始化
    const PageHandlerClass = PAGE_HANDLERS[this.currentPageType as SoPageType]
    if (PageHandlerClass) {
      PageHandlerClass.getInstance().init()
    }
    else if (this.currentPageType === UNDEFINED_PAGE_TYPE) {
      loggerDeduplicator.warn(UNDEFINED_PAGE_TYPE_TIPS)
    }
    else {
      logger.warn(`未找到 ${this.currentPageType} 页面的处理器`)
    }
  }

  platformStyleVar(_theme: ThemeType): string {
    return ``
  }

  getBaseStyles(_theme: ThemeType): string {
    return `
${super.getBaseStyles(_theme)}
`
  }

  getPageStyles(_theme: ThemeType): string {
    return `
${BODY_SELECTOR} {
  background-color: var(${this.themeVarService.search.default.background}) !important;
  color: var(${this.themeVarService.search.default.text}) !important;
  
  /* 隐藏热搜悬浮的分类标签 */
  &.search-input-fixed #main #card_container .top-tab {
    display: none !important;
  }
}
`
  }

  getMainStyles(theme: ThemeType): string {
    // 1. 通用样式
    let commonStyles = this.commonPage.getMainStyles()
    const notSupportedPages: SoPageType[] = [
      SoPageType.NotFound,
    ]

    if (notSupportedPages.includes(this.currentPageType as SoPageType))
      commonStyles = ''

    // 2. 动态获取当前页面样式
    let pageStyles = ''
    const PageHandlerClass = PAGE_HANDLERS[this.currentPageType as SoPageType]
    if (PageHandlerClass) {
      pageStyles = PageHandlerClass.getInstance().getMainStyles(theme)
    }

    return `
  /* ===================== 【页面】通用样式 ===================== */   
  ${commonStyles}
  /* ===================== 【页面】独立样式 ===================== */
  ${pageStyles}
`
  }

  getPatchedStyles(_theme: ThemeType): string {
    return ``
  }

  getFixedStyles(_theme: ThemeType): string {
    return ``
  }
}
